package com.esms.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author : [ko]
 * @version : [v1.0]
 * @description : [时间处理工具类]
 * @createTime : [2022/3/5 23:11]
 * @updateUser : [ko]
 * @updateTime : [2022/3/5 23:11]
 * @updateRemark : [说明本次修改内容]
 */

public class DateUtil {

	/**
	 * 计算当前日期是一年中第几季度：返回值：年+季度：2022+01/02/03/04
	 **/
	public static String GetQuarterByDate(String date) throws Exception {

		if (date == "" || "".equals(date)) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date datePar = sdf.parse(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(datePar);

		String year = String.valueOf(calendar.get(Calendar.YEAR));
		int mouth = datePar.getMonth() + 1;

		if (mouth >= 1 && mouth <= 3) {
			return year + "01";// 第一季度
		} else if (mouth >= 4 && mouth <= 6) {
			return year + "02";// 第二季度
		} else if (mouth >= 7 && mouth <= 9) {
			return year + "03";// 第三季度
		} else if (mouth >= 10 && mouth <= 12) {
			return year + "04";// 第四季度
		} else {
			return "000000";
		}
	}

	/**
	 * 根据日期字符串判断当月第几周
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getWeek(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		String week = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
		// 第几天，从周日开始
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		return year + "-" + month + "-" + week;
	}


	/**
	 * 根据日期字符串判断当月
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getMonthForNowDay(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		String week = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
		// 第几天，从周日开始
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		return month;
	}


	/**
	 * 根据日期字符串判断上个月
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	public static String getBefMonthForNowDay() throws Exception {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(formatter.format(currentTime));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		String week = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
		// 第几天，从周日开始
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH));
		return month;
	}



	/**
	 * 根据日期字符串判断当月第几周
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String getWeekForNowDay(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		String week = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
		return week;
	}

	public static void printWeeks() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		int month = calendar.get(Calendar.MONTH);
		int count = 0;
		while (calendar.get(Calendar.MONTH) == month) {
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				StringBuilder builder = new StringBuilder();
				builder.append("week:");
				builder.append(++count);
				builder.append(" (");
				builder.append(format.format(calendar.getTime()));
				builder.append(" - ");
				calendar.add(Calendar.DATE, 6);
				builder.append(format.format(calendar.getTime()));
				builder.append(")");
				System.out.println(builder.toString());
			}
			calendar.add(Calendar.DATE, 1);
		}
	}

	// 根据year年的第week周，查询本周的起止时间
	private static String weekToDayFormate(int year, int week){
		Calendar calendar = Calendar.getInstance();
		// ①.设置该年份的开始日期：第一个月的第一天
		calendar.set(year,0,1);
		// ②.计算出第一周还剩几天：+1是因为1号是1天
		int dayOfWeek = 7 - calendar.get(Calendar.DAY_OF_WEEK) + 1;
		// ③.周数减去第一周再减去要得到的周
		week = week - 2;
		// ④.计算起止日期
		calendar.add(Calendar.DAY_OF_YEAR,week * 7 + dayOfWeek);
		System.out.println("开始日期：" + new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
		calendar.add(Calendar.DAY_OF_YEAR, 6);
		System.out.println("结束日期：" + new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));

		return "";
	}


	//归属当年的哪一周
	private static int weekOfYear(String today){
		//String today = "2013-01-14";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(today);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);

		System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));

		return calendar.get(Calendar.WEEK_OF_YEAR);
	}

	/**当前日期所在周开始的第一天日期**/
	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		return c.getTime();
	}
	/**当前日期所在周结尾的最后一天日期**/
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		return c.getTime();
	}

	/**根据当前日期直接返回本周开始日期和结束日期**/
	public static String getFirstLastWeek(Date date){
		SimpleDateFormat formatterWeek = new SimpleDateFormat("yyyy年MM月dd日");
		String FirstWeekDay = formatterWeek.format(DateUtil.getFirstDayOfWeek(date));//当前日期本周开始第一天
		String LastWeekDay = formatterWeek.format(DateUtil.getLastDayOfWeek(date));//当前日期本周结尾最后一天
		String firstLastWeek = FirstWeekDay + " - " +LastWeekDay;;
		return firstLastWeek;
	}

	/**根据当前日期直接返回上个月的开始日期和结束日期**/
	public static String getBeforeMonDay(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		//获取前一个月第一天
		Calendar calendar1 = Calendar.getInstance();
		calendar1.add(Calendar.MONTH, -1);
		calendar1.set(Calendar.DAY_OF_MONTH,1);
		String firstDay = formatter.format(calendar1.getTime());
		//获取前一个月最后一天
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		String lastDay = formatter.format(calendar2.getTime());

		return firstDay+"-"+lastDay;
	}


	/** 测试代码 **/
	public static void main(String args[]) {
		Date currentTime = new Date();

		SimpleDateFormat formatterA = new SimpleDateFormat("yyyy年MM月dd");


		String firstLastWeek = DateUtil.getFirstLastWeek(currentTime);//当前日期本周开始第一天
		System.out.println("firstLastWeek==="+firstLastWeek);



		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);

		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);

		try {
			String aaa = GetQuarterByDate(dateString);
			String bbb = getWeek(dateString);
			System.out.println("第几季度===" + aaa);
			System.out.println("本月第几周===" + bbb);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}




		try {
			String today = "2022-03-08";
			String befMouth = DateUtil.getBefMonthForNowDay();

			System.out.println("上个月AAA===" + befMouth);

			String ddd = getWeekForNowDay(today);
			System.out.println("今天属于当月的第几周===="+ddd);
			String eee = getMonthForNowDay(today);
			System.out.println("今天属于第几月===="+eee);
			int weekYear = weekOfYear(today);
			System.out.println("今天属于当年的第几周===="+weekYear);
			System.out.println("这周是从那几天开始的==="+weekToDayFormate(2022, weekYear));
		}catch (Exception e){
			e.printStackTrace();
		}


		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		int month1 = calendar.get(Calendar.MONTH);
		int count = 0;
		while (calendar.get(Calendar.MONTH) == month1) {
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				StringBuilder builder = new StringBuilder();
				builder.append("week:");
				builder.append(++count);
				builder.append(" (");
				builder.append(format.format(calendar.getTime()));
				builder.append(" - ");
				calendar.add(Calendar.DATE, 6);
				builder.append(format.format(calendar.getTime()));
				builder.append(")");
				System.out.println(builder.toString());
			}
			calendar.add(Calendar.DATE, 1);
		}



		//获取前一个月第一天
		Calendar calendar1 = Calendar.getInstance();
		calendar1.add(Calendar.MONTH, -1);
		calendar1.set(Calendar.DAY_OF_MONTH,1);
		String firstDay = formatter.format(calendar1.getTime());
		//获取前一个月最后一天
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		String lastDay = formatter.format(calendar2.getTime());

		System.out.println("wocao==============="+firstDay+"-"+lastDay);



	}

}
